from flask import Blueprint,request,jsonify
from db import db1
from utils.comm import get_data
from utils.jwttoken import myjwt
from utils.acl import acl
from utils.redisconn import r
from utils.rbac import rbac


bp_user = Blueprint('user',__name__)


@bp_user.route('/login',methods=['POST'])
def login():
    data = get_data()
#     用户账号密码
#     print(data['acc'])
    sql = "select * from users where acc='%s'"%data['acc']
    res = db1.find(sql)
#     判断用户账号密码是否正常
    if data['acc'] == res['acc'] and data['password'] == res['password']:
#     生成token
        data= {'data':{'name':res['acc'],'uid':res['id']}}
        token = myjwt.jwt_encode(data)
#     获取页面权限
        page_def = acl.get_pagedef_reslist(res['id'])
        role_page_de = rbac.get_pagedef_reslist(res['id'])
        for i in page_def:
            if i not in role_page_de:
                role_page_de.append(i)
        # 获取后台权限
        role_inter = rbac.get_interface_reslist(res['id'])
        get_inter = acl.get_interface_reslist(res['id'])
        for i in get_inter:
            if i not in role_inter:
                role_inter.append(i)
        key = 'serve'
        r.insert_set(key+str(res['id']),role_inter)
        return jsonify({'code':200,'uid':res['id'],'token':token,'page_def':role_page_de,'username':res['acc']})
    else:
        return jsonify({"code":'40010','msg':'登陆失败'})




